package com.txunda.zhuohired.aty.user.resume;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.tools.AlertDialog;
import com.ants.theantsgo.util.JSONUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.adapter.user.resume.ProjectAdapter;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.bean.user.resume.ProjectBean;
import com.txunda.zhuohired.http.pst.ResumePst;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/7 10:28
 * 功能描述：项目经验
 * 联系方式：jiahuanchen@126.com
 */

public class ProjectAty extends BaseAty {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_white_center)
    private TextView tv_layout_white_center;
    /**
     * 编辑
     */
    @ViewInject(R.id.tv_layout_white_right)
    private TextView tv_layout_white_right;
    /**
     * listView
     */
    @ViewInject(R.id.lv_project)
    private ListView lv_project;
    /**
     * 空数据
     */
    @ViewInject(R.id.ll_no_data)
    private LinearLayout ll_no_data;

    private ProjectAdapter projectAdapter;
    private int projectStatus = 1;
    private ResumePst resumePst;
    private String token = "";
    private List<ProjectBean.DataBean> projectList;
    private String model = "UserJob";//UserPractice.工作经历，UserEducation.教育经历，UserJob.项目经历，Social.社交主页

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_white_head);
        tv_layout_white_center.setText("项目经验");
        tv_layout_white_right.setVisibility(View.VISIBLE);
        tv_layout_white_right.setText("编辑");
    }

    @Override
    @OnClick({R.id.tv_project_add, R.id.tv_layout_white_right})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_project_add://添加项目经验
                startActivity(AddProjectAty.class, null);
                break;
            case R.id.tv_layout_white_right://编辑/完成
                if (projectStatus == 1) {
                    projectStatus = 2;
                    tv_layout_white_right.setText("完成");
                    initAdapter();
                } else {
                    projectStatus = 1;
                    tv_layout_white_right.setText("编辑");
                    initAdapter();
                }
                break;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_project;
    }

    @Override
    protected void initialized() {
        resumePst = new ResumePst(this);
        token = application.getUserInfo().get("data");
        projectList = new ArrayList<>();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        initHttp();
    }

    @Override
    protected void requestData() {
        initHttp();
    }

    private void initHttp() {
        resumePst.job(token);
    }

    private void initAdapter() {
        projectAdapter = new ProjectAdapter(this, projectStatus, projectList);
        lv_project.setAdapter(projectAdapter);
        projectAdapter.SetOnClickListener(new ProjectAdapter.HolderClickListener() {
            @Override
            public void onSureHolderClick(int position) {
                onShowDialog(projectList.get(position).getId());
            }
        });
    }

    private void onShowDialog(final String id) {
        new AlertDialog(this).builder().setTitle("提示").setMsg("确认删除该项目经验?")
                .setPositiveButton("确认", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        //                        L.e("======id======",id);
                        //                        L.e("======model======",model);
                        resumePst.delPractice(id, model);
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        }).show();
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("job")) {
            //            L.e("=====job======", jsonStr);
            ProjectBean projectBean = GsonUtil.GsonToBean(jsonStr, ProjectBean.class);
            projectList = projectBean.getData();
            if (projectList.size() == 0) {
                noData(2);
            } else {
                noData(1);
                initAdapter();
            }
            return;
        }
        if (requestUrl.contains("delPractice")) {
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            showRightTip(map.get("message"));
            projectStatus = 1;
            tv_layout_white_right.setText("编辑");
            initHttp();
            return;
        }
    }

    /**
     * @param type 1 有数据 2 无数据
     */
    private void noData(int type) {
        if (type == 1) {
            lv_project.setVisibility(View.VISIBLE);
            ll_no_data.setVisibility(View.GONE);
        } else {
            lv_project.setVisibility(View.GONE);
            ll_no_data.setVisibility(View.VISIBLE);
        }
    }
}
